package com.gemme.gemme.view;

import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gemme.gemme.R;
import com.gemme.gemme.wode.AttributeSpecListBean;
import com.nex3z.flowlayout.FlowLayout;

import java.util.List;

public class GuigeSingleView extends RelativeLayout {
    private Context context;
    private OnSelectReturn onSelectReturn;
    private AttributeSpecListBean bean;
    private String name;

    public GuigeSingleView(Context context) {
        this(context,null);
    }

    public GuigeSingleView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public GuigeSingleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
//        initData();
    }



    FlowLayout flow;
    ImageView iv_cover; //封面
    TextView tv_name; //名称
    TextView tv_payPrice; //支付金额
    TextView tv_oldPrice; //原价
    TextView tv_kucun; //库存
    TextView tv_guige; //已选
    TextView tv_confirm;
    PriceView priceview;

    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.guige_single_view,this);
        iv_cover = findViewById(R.id.iv_cover);
        tv_name = findViewById(R.id.tv_name);
        tv_payPrice = findViewById(R.id.tv_payPrice);
        tv_oldPrice = findViewById(R.id.tv_oldPrice);
        tv_kucun = findViewById(R.id.tv_kucun);
        tv_guige = findViewById(R.id.tv_guige);
        flow = findViewById(R.id.flow);
        tv_confirm = findViewById(R.id.tv_confirm);
        priceview = findViewById(R.id.priceview);
        tv_confirm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onSelectReturn != null){
                    onSelectReturn.OnSelectReturn(bean,priceview.getNum());
                }
            }
        });
    }

/*
    private void initData() {
        ArrayList<String> dataList = new ArrayList<String>();
        dataList.add("040黑金");
        dataList.add("040黑红");
        dataList.add("39");
        dataList.add("40");
        dataList.add("4041");
        dataList.add("4大萨达撒多");
        dataList.add("4大上的反复");
        dataList.add("4电商");
        dataList.add("4电商地方撒大");
        for (int i = 0; i < dataList.size(); i++) {
            GuigeTextView textView = new GuigeTextView(context);

            textView.setContent(dataList.get(i) + "");
            flow.addView(textView);
        }
    }
*/


    public void setData(List<AttributeSpecListBean> list,String productName) {

        try {
            name = productName;
            for (int i = 0; i < list.size(); i++) {
                AttributeSpecListBean bean = list.get(i);
                GuigeTextView textView = new GuigeTextView(context);
                textView.setTag(bean);
                textView.setContent(getName(bean) + "");
                flow.addView(textView);

                textView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        AllViewNoSelect();
                        boolean isSelect = !textView.getIsSelect();
                        textView.setIsSelect(isSelect);
//                        textView.getTag();
                        setSelect(bean);

                        /*if(onSelectReturn != null){
                            onSelectReturn.OnSelectReturn(bean,0);
                        }*/

                    }
                });

                if(i == 0){ //进来默认选择第一个
                    textView.setIsSelect(true);
                    setSelect(bean);
                }
            }
        }catch (Exception e){

        }
    }

    private void setSelect(AttributeSpecListBean bean) {
        this.bean = bean;
        try {
            tv_name.setText(name);
            tv_payPrice.setText("¥" + (bean.getDiscountPrice() <= 0 ? bean.getAttributeSpecPrice() : bean.getDiscountPrice() + ""));
            tv_oldPrice.setText("¥" + bean.getAttributeSpecPrice() + "");
            tv_oldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            tv_kucun.setText("库存：" + bean.getStockNum());
            tv_guige.setText("已选: " + getName(bean));
            Glide.with(context).load(bean.getAttributeSpecImage()).into(iv_cover);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void AllViewNoSelect() {
       try {
           for (int i = 0; i < flow.getChildCount(); i++) {
               GuigeTextView textView = (GuigeTextView)flow.getChildAt(i);
               textView.setIsSelect(false);
           }
       }catch (Exception e){

       }
    }

    private String getName(AttributeSpecListBean bean) {
       String name= "";
       try {
           List<AttributeSpecListBean.PropertyListBean> propertyList = bean.getPropertyList();
           for (int i = 0; i < propertyList.size(); i++) {
               AttributeSpecListBean.PropertyListBean pBean = propertyList.get(i);
               String attributeName = pBean.getAttributeName();
               String attributeSpecName = pBean.getAttributeSpecName();

               if(TextUtils.isEmpty(name)){
                   name = name + attributeName +"+"+ attributeSpecName;
               }else{
                   name = name +"+"+ attributeName +"+"+ attributeSpecName;
               }

           }
       }catch (Exception e){

       }

       return name;
    }


    public  GuigeSingleView setOnSelectReturn(OnSelectReturn onSelectReturn){
        this.onSelectReturn = onSelectReturn;
        return this;
    }

    public interface OnSelectReturn{
        void OnSelectReturn(AttributeSpecListBean bean,int num);
    }
}
